// pages/shop/shop.js

const app = getApp();
var mathUtils = require('../../utils/mathUtil.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    tabbarList: app.globalData.tabbarList,

    swiperList: [],
    drugType: [],
    drugInfos: []
  },

  onSearch(event) {
    console.log("onSearch")
  },

  onClickSwiperItem(event) {
    console.log("onClickSwiperItem", event);
    const drugInfo = event.currentTarget.dataset.item;
    
    wx.navigateTo({
      url: '/pages/shop/drug/drug?drugInfo=' + encodeURIComponent(JSON.stringify(drugInfo))
    });
  },

  onClickGoodType(event) {
    console.log("onClickGoodType", event);

    let type = event.currentTarget.dataset.item.type;
    console.log("type is: ", type);

    let drugList = this.data.drugInfos;
    var param = [];
    for (var i = 0; i < drugList.length; i++) {
      if (drugList[i].type == type) {
        param.push(drugList[i]);
      }
    }
    
    console.log("param is: ", param);

    wx.navigateTo({
      url: '/pages/shop/drugList/drugList?drugList=' + encodeURIComponent(JSON.stringify(param))
    });
  },

  onClickGood(event) {
    console.log("onClickGood", event);

    let drugInfo = event.currentTarget.dataset.item;
    console.log("param drugInfo is: ", drugInfo);

    wx.navigateTo({
      url: '/pages/shop/drug/drug?drugInfo=' + encodeURIComponent(JSON.stringify(drugInfo))
    });
  },

  onClickTabbar(event) {
    console.log("onClickTabbar", event);
    const url = this.data.tabbarList[event.detail].url;

    wx.switchTab({
      url: url,
    });
  },

  init() {
    wx.showLoading({
      title: '数据加载中',
    });

    var that = this;

    // 初始化药品类型
    wx.cloud.callFunction({
      name: "get_drug_type",
      success: res => {
        console.log("get_drug_type success", res);

        that.setData({
          drugType: res.result.data
        });
      },
      fail: res => {
        console.log("get_drug_type fail", res);
      }
    });

    // 初始化药品信息
    wx.cloud.callFunction({
      name: "get_drug_info",
      success: res => {
        console.log("get_drug_info success", res);
        const drugInfos = res.result.data;

        that.setData({
          drugInfos: drugInfos
        });

        // 初始化轮播图列表，从药品信息中随机获取5个
        const size = drugInfos.length;
        const randomVec = mathUtils.getRandomValues(0, size - 1, 5);        
        const swiperList = [];
        for (let i = 0; i < 5; i++) {
          swiperList.push(drugInfos[randomVec[i]]);
        }

        that.setData({
          swiperList: swiperList
        });

        console.log(randomVec, swiperList);
        wx.hideLoading();
      },
      fail: res => {
        console.log("get_drug_info fail", res);
      }
    });
  },

  // 生命周期函数--监听页面初次渲染完成
  onLoad(options) {
    this.init();
  },

  // 生命周期函数--监听页面初次渲染完成
  onReady() { },

  // 生命周期函数--监听页面显示
  onShow() { },

  // 生命周期函数--监听页面隐藏
  onHide() { },

  // 生命周期函数--监听页面卸载
  onUnload() { },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() { 
    this.init();
  },

  // 页面上拉触底事件的处理函数
  onReachBottom() { },

  // 用户点击右上角分享
  onShareAppMessage() { }
})